#include "MediaCenter.h"
#include "Player.h"
#include <algorithm>
using namespace std;

bool MediaCenter::PlayerChanged(Player* player, const string& action) {
	if (find(players.begin(), players.end(), player) == players.end()) {
		return false;
	}
	
	if (action.compare("start") == 0) {
		if (activePlayer != player) { // Player is not the active one
			if (activePlayer != 0) {
				activePlayer->stop();
			}
			
			activePlayer = player;
		}
		
		return true;
	}
	
	else if (action.compare("stop") == 0) {
		return (activePlayer == player);
	}
	
	else if (action.compare("forward") == 0) {
		return (activePlayer == player);
	}
	
	else if (action.compare("rewind") == 0) {
		return (activePlayer == player);
	}
	
	return false;
}

void MediaCenter::addPlayer(Player* player) {
	players.push_back(player);
}

void MediaCenter::removePlayer(Player* player) {
	players.remove(player);
}

void MediaCenter::start(Player* player) {
	if (find(players.begin(), players.end(), player) == players.end()) {
		return;
	}
	
	player->start();
}

void MediaCenter::stop() {
	if (activePlayer != 0) {
		activePlayer->stop();
	}
}

void MediaCenter::forward() {
	if (activePlayer != 0) {
		activePlayer->forward();
	}
}

void MediaCenter::rewind() {
	if (activePlayer != 0) {
		activePlayer->rewind();
	}
}